﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MuYue;
using MuYue.Common;
using MuYue.Config;
using MuYue.ORM;

namespace MuYue.Config
{
    /// <summary>
    /// 使用SQLiteORM来读写配置
    /// </summary>
    internal class AppConfigReadWriteProvider : IDisposable
    {

        #region 本类才会用到的私有类型
        /// <summary>
        /// 配置写入方式枚举
        /// </summary>
        private enum ConfigWriteMethod
        {
            Update,
            Insert
        }

        /// <summary>
        /// 封装配置类 来实现传递更新的方式
        /// </summary>
        private class ConfigThreadModel
        {
            public ConfigThreadModel(ConfigWriteMethod configWriteMethod, AppConfigModel config) {
                this.Method = configWriteMethod;
                this.Model = config;
            }
            public ConfigWriteMethod Method { private set; get; }
            public AppConfigModel Model { private set; get; }
        }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public AppConfigReadWriteProvider() {
            SqlQuene = new ThreadModel<ConfigThreadModel>();
            SqlQuene.DealModelEvent += SqlQuene_DealModelEvent;
        }

        /// <summary>
        /// 异步写入队列
        /// </summary>
        private ThreadModel<ConfigThreadModel> SqlQuene;

        /// <summary>
        /// 异步处理方法
        /// </summary>
        /// <param name="model"></param>
        private void SqlQuene_DealModelEvent(ConfigThreadModel model) {
            switch (model.Method) {
                case ConfigWriteMethod.Insert:
                    FastDb.Add(model.Model);
                    break;
                case ConfigWriteMethod.Update:
                    FastDb.Update(model.Model, "MangerName", "Key");
                    break;
            }
        }

        #region 外部事件连接函数
        /// <summary>
        /// 读取所有配置项的SQL操作
        /// </summary>
        /// <param name="managerName"></param>
        /// <returns></returns>
        public List<AppConfigModel> AppConfigManager_SqliteReadEvent(string managerName) {
            return FastDb.GetAll<AppConfigModel>(string.Format("`MangerName`='{0}'", managerName));
        }

        /// <summary>
        /// 更新配置项的SQL操作
        /// </summary>
        /// <param name="config"></param>
        public void AppConfigManager_UpdateConfigItemEvent(AppConfigModel config) {
            SendToQuene(config, ConfigWriteMethod.Update);
        }

        /// <summary>
        /// 消息队列的方式进行异步处理时间占用较长的Sqlite写入
        /// </summary>
        /// <param name="config"></param>
        /// <param name="configWriteMethod"></param>
        private void SendToQuene(AppConfigModel config, ConfigWriteMethod configWriteMethod) {
            SqlQuene.Append(new ConfigThreadModel(configWriteMethod, config));
        }


        /// <summary>
        /// 新增配置项的SQL操作
        /// </summary>
        /// <param name="config"></param>
        public void AppConfigManager_InsertConfigItemEvent(AppConfigModel config) {
            SendToQuene(config, ConfigWriteMethod.Insert);
        }
        #endregion

        #region IDisposable 成员

        /// <summary>
        /// 释放占用的资源
        /// 关闭当前处理端的消息队列
        /// </summary>
        public void Dispose() {
            SqlQuene.Dispose();
        }

        #endregion
    }
}
